#include <stdio.h>
int main()
{
    int n,a,f(char);
    char b[2];
    scanf("%d",&n);
    while (n--)
    {
        scanf("%s%d",b,&a);
        printf("%d\n",a+f(b[0]));
    }
    return 0;
}
int f(char b)
{
    if (b>='A'&&b<='Z')return b-'A'+1;
    else return -(b-'a'+1);
}
